package com.wkk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wkk.entity.User;
import com.wkk.service.UserService;

@org.springframework.stereotype.Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {
	@Resource
	private UserService userService;

	@RequestMapping("/save")
	@ResponseBody
	public Integer saveshuxing(String name, String zi, String sex, Integer wuli, Integer tongshuai, Integer zhili,
			Integer zhengzhi, Integer meili) {
		if (wuli + zhili + tongshuai + zhengzhi + meili > 300 || wuli > 80 || tongshuai > 80 || zhili > 80
				|| zhengzhi > 80 || meili > 80) {
			return 0;
		}
		if (sex.equals("男")) {
			wuli += 10;
		} else {
			meili += 10;
		}
		Integer yunqi = (int) (Math.random() * 10);
		User user = new User();
		user.setName(name);
		user.setMeili(meili);
		user.setWuli(wuli);
		user.setZhili(zhili);
		user.setZhengzhi(zhengzhi);
		user.setYunqi(yunqi);
		user.setZi(zi);
		user.setSex(sex);
		user.setTongshuai(tongshuai);
		user.setShuxing(1);
		userService.add(user);
		return user.getId();

	}

	@RequestMapping("/update")
	@ResponseBody
	public int update(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("kingdom_id", (req.getParameter("kingdom_id")) != null ? req.getParameter("kingdom_id") : null);
		map.put("user_id", req.getParameter("user_id") != null ? req.getParameter("user_id") : null);
		map.put("station_id", req.getParameter("station_id") != null ? req.getParameter("station_id") : null);
		map.put("gongxun", req.getParameter("gongxun") != null ? Integer.parseInt(req.getParameter("gongxun")) : null);
		int count = userService.update(map);
		return count;

	}

	@RequestMapping("/list")
	@ResponseBody
	public List<User> list(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", req.getParameter("user_id") != null ? req.getParameter("user_id") : null);
		List<User> lists = userService.list(map);
		
		return lists;

	}
}
